﻿using System;

namespace NetTrafficStatistics
{
	class Params
	{
		internal int[] Link = new int[]{};
		internal string[] Path = new string[]{};
		internal string Where = "1";
		internal string SelectEx;
		internal string GroupBy = "time";
		internal string HeaderType = "HH:00";
		internal bool Custom;

		internal Params (int index, int zone)
		{
			var dt = DateTime.Now;

			var y = dt.ToString ("yyyy");
			var y1 = dt.AddYears (-1).ToString ("yyyy");
			var y2 = dt.AddMonths (-1).ToString ("yyyy");
			
			var m = dt.ToString ("MMMM");
			var m1 = dt.AddMonths (-1).ToString ("MMMM");
			var m2 = dt.AddDays (-1).ToString ("MMMM");
			
			var dd = dt.ToString ("dd, ddd");
			var dd1 = dt.AddDays (-1).ToString ("dd, ddd");

			switch (index)
			{
				case 0:
					// Today
					Path = new[] { y, m, dd };
					Link = new[] { 6, 4 };
					HeaderType = "HH:00";

					// SQL
					Where = "zone=" + zone + " AND date(time) LIKE date('now')";
					break;

				case 1:
					// Yesterday
					Path = new[] { y, m2, dd1 };
					Link = new[] { 6, m == m2 ? 4 : 5 };
					HeaderType = "HH:00";

					// SQL
					Where = "zone=" + zone + " AND date(time) LIKE date('now','-1 day')";
					break;

				case 2:
					// This week
					var wdt = dt.AddDays (1 - (int) dt.DayOfWeek);
					var wdt1 = dt.AddDays (7 - (int) dt.DayOfWeek);

					if (wdt.Month == wdt1.Month)
					{
						Path = new[] { y, m, string.Format ("{0} - {1}", wdt.ToString ("dd, ddd"), wdt1.ToString ("dd, ddd")) };
						Link = new[] { 6, 4 };
					}
					else
					{
						Path = new[] { y, string.Format ("{0} - {1}", wdt.ToString ("dd, MMM"), wdt1.ToString ("dd, MMM")) };
						Link = new[] { 6 };
					}

					HeaderType = "dd, ddd";

					// SQL
					Where = "zone=" + zone + " AND (date(time) >= date('now','-6 day','weekday 1')) AND (date(time) <= date('now','weekday 0'))";
					SelectEx = ", strftime('%d',time) as d";
					GroupBy = "d";
					break;

				case 3:
					// Last week
					wdt = dt.AddDays (1 - (int) dt.DayOfWeek).AddDays (-7);
					wdt1 = dt.AddDays (7 - (int) dt.DayOfWeek).AddDays (-7);

					if (wdt.Month == wdt1.Month)
					{
						Path = new[]{y, wdt.ToString ("MMMM"), string.Format ("{0} - {1}", wdt.ToString ("dd, ddd"), wdt1.ToString ("dd, ddd"))};
						Link = new[] { 6, wdt.Month == dt.Month ? 4 : 5 };
					}
					else
					{
						Path = new[] { y, string.Format ("{0} - {1}", wdt.ToString ("dd, MMM"), wdt1.ToString ("dd, MMM")) };
						Link = new[] { 6 };
					}

					HeaderType = "dd, ddd";

					// SQL
					Where = "zone=" + zone + " AND (date(time) >= date('now','-6 day','weekday 1','-7 day')) AND (date(time) <= date('now','weekday 0','-7 day'))";
					SelectEx = ", strftime('%d',time) as d";
					GroupBy = "d";
					break;

				case 4:
					// This month
					Path = new[] { y, m };
					Link = new[] { 6 };
					HeaderType = "dd, ddd";

					// SQL
					Where = "zone=" + zone + " AND (date(time) >= date('now','start of month')) AND (date(time) < date('now','start of month','+1 month'))";
					SelectEx = ", strftime('%d',time) as d";
					GroupBy = "d";
					break;

				case 5:
					// Last month
					Path = new[] { y2, m1 };
					Link = new[] { y == y2 ? 6 : 7 };
					HeaderType = "dd, ddd";

					// SQL
					Where = "zone=" + zone + " AND (date(time) >= date('now','start of month','-1 month')) AND (date(time) < date('now','start of month'))";
					SelectEx = ", strftime('%d',time) as d";
					GroupBy = "d";
					break;

				case 6:
					// This year
					Path = new[] { y };
					HeaderType = "MMMM";

					// SQL
					Where = "zone=" + zone + " AND (date(time) >= date('now','start of year')) AND (date(time) < date('now','start of year','+1 year'))";
					SelectEx = ", strftime('%m',time) as d";
					GroupBy = "d";
					break;

				case 7:
					// Last year
					Path = new[] { y1 };
					HeaderType = "MMMM";

					// SQL
					Where = "zone=" + zone + " AND (date(time) >= date('now','start of year','-1 year')) AND (date(time) < date('now','start of year'))";
					SelectEx = ", strftime('%m',time) as d";
					GroupBy = "d";
					break;

				case 8:
					// All year
					HeaderType = "yyyy";

					// SQL
					Where = "zone=" + zone;
					SelectEx = ", strftime('%Y',time) as d";
					GroupBy = "d";
					break;

				case 9:
					Custom = true;
					break;
			}
		}
	}
}
